﻿#include "loginwindow.h"
#include "ui_loginwindow.h"
#include "flatui.h"
#include "mainwindow.h"
#include <QPainter>
#include <QFile>
#include <QDebug>
#include <QSettings>
#include "globdataobj.h"
#include "testdialog.h"
#include <QMessageBox>
#include <QDesktopWidget>
#include <QCryptographicHash>



LoginWindow::LoginWindow(Dbtools *db, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::LoginWindow)
{
    ui->setupUi(this);

    ui->labelCloud->setStyleSheet("color:#2828FF;");

    //窗口关闭时，释放内存
    //this->setAttribute(Qt::WA_DeleteOnClose);
    dbtools = db;

    //设置图标
    this->setWindowIcon(QPixmap(":/new/prefix1/res/w128X128.ico"));
    this->setWindowTitle("OnePassX");

    QPixmap pix;
    pix.load(":/new/prefix1/res/OnePass.png");
    //pix = pix.scaled(180,160,Qt::KeepAspectRatio);


    ui->label_logo->setPixmap(pix);
    ui->label_logo->setScaledContents(true);

    //设置窗口不能最大化
    this->setWindowFlags(this->windowFlags()&~Qt::WindowMaximizeButtonHint);

    //加载样式
    //QFile qss(":/new/prefix1/res/login.css");
    //qss.open(QFile::ReadOnly);
    ////qApp->setStyleSheet(qss.readAll());
    //this->setStyleSheet(qss.readAll());
    //qss.close();

    ui->pushButton->setFixedSize(100,30);
    FlatUI::Instance()->setPushButtonQss(ui->pushButton,5, 8,
                                   "#1ABC9C", "#E6F8F5", "#2EE1C1", "#FFFFFF", "#16A086", "#A7EEE6");
    FlatUI::Instance()->setLineEditQss(ui->lineEdit);
    ui->lineEdit->setEchoMode(QLineEdit::Password);
    ui->lineEdit->setFixedWidth(200);

    //事件处理
    connect(ui->pushButton,&QPushButton::clicked,[=](){
        login();

    });
    //QLabel本身没有单击事件，通过为其安装事件过滤器来实现
    ui->labelCloud->installEventFilter(this);
    ui->labelResetSetting->installEventFilter(this);

    //保存配置文件
    ///QSettings settings("./config.ini",QSettings::IniFormat);
    ///settings.setValue("firstRun","Y");

    //settings.sync();//保存



    QSettings setting;
    if(setting.value("cloud/account").isValid() && setting.value("cloud/account").toString().count()>1){
        //已激活云服务不需要显示
        ui->labelCloud->setVisible(false);
    }

}
void LoginWindow::paintEvent(QPaintEvent *event){
    //创建画家，指定绘图设备
    QPainter painter(this);
    //创建QPixmap对象
    QPixmap pix;
    //加载图像
    pix.load(":/new/prefix1/res/W13.jpg");
    //绘制背景图
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
}
void LoginWindow::keyPressEvent(QKeyEvent *event){
    if(event->key()==Qt::Key_Return){
        login();
    }
}

void LoginWindow::login()
{
    //判断密码是否输入正确
    QString s = ui->lineEdit->text();
    QString ss = QString(QCryptographicHash::hash(s.toUtf8(),QCryptographicHash::Sha256).toHex());
    QSettings sett;
    if(sett.value("app/mainpass").isValid() && sett.value("app/mainpass").toString() == ss){
        //设置Key
        key = ss;
        MainWindow *mainWindow=new MainWindow(dbtools);
        mainWindow->show();
        close();
    }else{
        QMessageBox::critical(this,"错误","密码错误",QMessageBox::Ok,QMessageBox::Ok);
    }
}

void LoginWindow::openCloudPage()
{
    qDebug()<<"opencloudpage";
}

void LoginWindow::resetSetting()
{
    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(this,tr("提示"),tr("确定要重置所有设置吗？"),QMessageBox::Yes|QMessageBox::No);
    if(reply == QMessageBox::Yes){
        qDebug()<<"确定要重置所有设置";
        QSettings sett;
        sett.clear();
        QApplication *app;
        app->exit(773);
    }else{
        qDebug()<<"取消重置操作";
    }
}

bool LoginWindow::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->labelCloud && event->type()==QEvent::MouseButtonPress){
        openCloudPage();
    }else if(watched == ui->labelResetSetting && event->type() == QEvent::MouseButtonPress){
        resetSetting();
    }

    return QMainWindow::eventFilter(watched,event);
}

LoginWindow::~LoginWindow()
{
    delete ui;
}
